import { changeThemeColor, getLocalSetting } from '@/utils/themeUtil'
import { message } from 'antd'
import { 
  COUNT,
  THEME,
  SETTING,
  THEMECOLOR,
  LAYOUT,
  FIXSIDE,
  FIXHEADER,
  MULTIPAGE,
  COLORWEAKNESS,
  GRAYNESS,
  HEADERPROGRESS,
  FORBIDANIMATION,
  ANIMATIONTYPE,
  ANIMATIONDIRECTION,
  COLLAPSESTATUS,
  INJECTTRANSITION,
  ACTIVEMENUITEM
} from './types'
import { addClass, removeClass } from '@/utils/toolUtils'


const localSetting = getLocalSetting(false)
const localStorageKey = process.env.REACT_APP_SETTING

const reducer = (state, action) => {
  switch (action.type) {
    case COUNT:
      const count = action.value;
      return {...state, ...{ count }}
    case ACTIVEMENUITEM:
      const activeMenuItem = action.value;
      return {...state, ...{ activeMenuItem }}
    // 设置抽屉开关
    case SETTING:
      const setting = !state.setting
      return {...state, ...{ setting }}
    // 设置侧边收缩状态
    case COLLAPSESTATUS:
      const collapseStatus = !state.collapseStatus
      return {...state, ...{ collapseStatus }}
    // 设置注入过渡
    case INJECTTRANSITION:
      const injectTransition = action.value
      return {...state, ...{ injectTransition }}
    // 修改风格
    case THEME: 
      const theme = action.value
      changeThemeColor(state.themeColor, theme).then(message.info('修改了主题，正在切换'))
      localSetting.theme = theme
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ theme }}
    // 修改主题色
    case THEMECOLOR:
      const themeColor = action.value
      changeThemeColor(themeColor, state.theme).then(message.info('修改了主题色，正在切换'))
      localSetting.themeColor = themeColor
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ themeColor }}
    // 修改布局
    case LAYOUT:
      const layout = action.value
      localSetting.layout = layout
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ layout }}
    // 固定侧边栏
    case FIXSIDE:
      const fixSider = !state.fixSider
      localSetting.fixSider = fixSider
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ fixSider }}
    // 固定头部
    case FIXHEADER:
      const fixHeader = !state.fixHeader
      localSetting.fixHeader = fixHeader
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ fixHeader }}
    // 多页标签
    case MULTIPAGE:
      const multiPage = !state.multiPage
      localSetting.multiPage = multiPage
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ multiPage }}
    // 色弱模式
    case COLORWEAKNESS:
      const colorWeakness = !state.colorWeakness
      colorWeakness ? addClass(document.documentElement, 'color-weak') : removeClass(document.documentElement, 'color-weak')
      localSetting.colorWeakness = colorWeakness
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ colorWeakness }}
    // 灰色模式
    case GRAYNESS:
      const grayMode = !state.grayMode
      grayMode ? addClass(document.documentElement, 'gray-mode') : removeClass(document.documentElement, 'gray-mode')
      localSetting.grayMode = grayMode
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ grayMode }}
    // 顶部进度条
    case HEADERPROGRESS:
      const headerProgress = !state.headerProgress
      localSetting.headerProgress = headerProgress
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ headerProgress }}
    // 禁用动画
    case FORBIDANIMATION:
      const forbidAnimation = !state.forbidAnimation
      localSetting.forbidAnimation = forbidAnimation
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ forbidAnimation }}
    // 动画类型
    case ANIMATIONTYPE:
      const animationType = action.value
      localSetting.animationType = animationType
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ animationType }}
    // 动画方向
    case ANIMATIONDIRECTION:
      const animationDirection = action.value
      localSetting.animationDirection = animationDirection
      localStorage.setItem(localStorageKey, JSON.stringify(localSetting))
      return {...state, ...{ animationDirection }}
    default:
      return state;
  }
};
export default reducer